//
//  HomeListCell.swift
//  SwitfTest
//
//  Created by runone02 on 2018/12/28.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

import UIKit

class HomeListCell: UITableViewCell {
    // 定义闭包
    typealias callBlock = (_ index:Int,_ str:String)->()
    // 设置闭包属性
    var sendValueBlock:callBlock!
    // 无参数闭包的定义
    typealias otherBlock = ()->()
    var noneParmBlock:otherBlock!

    /// 标题
    var titleLabel:UILabel!
    /// 正文
    var contentLabel: UILabel!
    /// 图片
    var iconImageView:UIImageView!
    /// 底部标题    1
    var bottomLabel:UILabel!
    /// 时间
    var timeLabel:UILabel!
    /// 删除按钮
    var deleteButton:UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.selectionStyle = UITableViewCell.SelectionStyle(rawValue: 0)!
        self.separatorInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: -10)

        setUpUI()
    }
    
    func setUpUI() {
        titleLabel = UILabel()
        titleLabel.font = UIFont.systemFont(ofSize: 15)
        titleLabel.text = "标题"
        titleLabel.textColor = UIColor.black
        self .addSubview(titleLabel)
        titleLabel .mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(10)
            make?.top.mas_equalTo()(10)
            make?.height.mas_equalTo()(10)
        }

        bottomLabel = UILabel()
        bottomLabel.textColor = UIColor.black
        bottomLabel.font = UIFont.systemFont(ofSize: 15)
        bottomLabel.text = "底部文字"
        self.addSubview(bottomLabel)
        bottomLabel.mas_makeConstraints { (maek) in
            maek?.bottom.mas_equalTo()(self)?.offset()(-5)
            maek?.left.mas_equalTo()(10)
            maek?.width.mas_equalTo()(80)
            maek?.height.mas_equalTo()(15)
        }
        
        timeLabel = UILabel()
        timeLabel.text = "时间"
        timeLabel.font = UIFont.systemFont(ofSize: 15)
        timeLabel.textColor = UIColor.black
        self.addSubview(timeLabel)
        timeLabel .mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(bottomLabel.mas_right)?.offset()(5)
            make?.centerY.mas_equalTo()(bottomLabel)
            make?.height.mas_equalTo()(15)
            make?.width.mas_equalTo()(80)
        }
        
        deleteButton = UIButton(type: .custom)
        deleteButton.setTitle("删除", for: .normal)
        deleteButton.backgroundColor = UIColor.darkGray
        deleteButton.setTitleColor(UIColor.black, for: .normal)
        deleteButton.addTarget(self, action: #selector(deleteButtonClick), for: .touchUpInside)
        self.addSubview(deleteButton)
        deleteButton.mas_makeConstraints { (make) in
            make?.right.mas_equalTo()(-5)
            make?.bottom.mas_equalTo()(bottomLabel)
            make?.width.mas_equalTo()(40)
            make?.height.mas_equalTo()(40)
        }
        
        iconImageView = UIImageView()
        iconImageView.backgroundColor = UIColor.red
        self.addSubview(iconImageView)
        iconImageView.mas_makeConstraints { (maek) in
            maek?.right.mas_equalTo()(self)?.offset()(-5)
            maek?.width.mas_equalTo()(50)
            maek?.top.mas_equalTo()(10)
            maek?.bottom.mas_equalTo()(deleteButton.mas_top)?.offset()(-5)
        }
   
        contentLabel = UILabel()
        contentLabel.font = UIFont.systemFont(ofSize: 14)
        contentLabel.text = "16:55:38.834358+0800 SwitfTest[14707:5774286] [MobSDK-WARNING]请在Info.plist文件中配置你的MOBAppKey和MOBAppSecret或者调用registerAppKey:appSecret:方法"
        contentLabel.textColor = UIColor.black
        contentLabel.numberOfLines = 4
        self.addSubview(contentLabel)
        contentLabel .mas_makeConstraints { (maek) in
            maek?.left.mas_equalTo()(titleLabel.mas_left)
            maek?.right.mas_equalTo()(iconImageView.mas_left)?.offset()(-5)
            maek?.top.mas_equalTo()(titleLabel.mas_bottom)?.offset()(5)
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
    @objc func deleteButtonClick(sender:UIButton){
        if (self.sendValueBlock != nil) {
            self.sendValueBlock(1,(sender.titleLabel?.text)!)
        }
        
        if (self.noneParmBlock != nil ){
            //无参闭包
            self.noneParmBlock()
        }

    }

}
